/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.tree;

import java.awt.Component;
import java.awt.Font;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

public class CatalogTreeCellRenderer implements TreeCellRenderer {
	private static final long serialVersionUID = -5750812344479325305L;
	
	@Override
	public Component getTreeCellRendererComponent(JTree tree, 
			Object value, 
			boolean selected, 
			boolean expanded, 
			boolean leaf, 
			int row, 
			boolean hasFocus) {
		
		/* create a label using default tree cell renderer */
		DefaultTreeCellRenderer dtcr = new DefaultTreeCellRenderer();		
		dtcr.getTreeCellRendererComponent(tree, 
				value, 
				selected, 
				expanded, 
				leaf, 
				row, 
				hasFocus);
		
		/* set font */
		dtcr.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		/* set icons */
		Icon icon = null;
		
		/* workspace */
		if(value instanceof CatalogRootTreeNode) {			
			icon = getRessourceIcon("database.png");
		/* reviewer-root */
		} else if(value instanceof AspectsRootTreeNode) {		
			icon = getRessourceIcon("User-Group-24x24.png");
		/* catalog */
		} else if(value instanceof CatalogTreeNode) {
			if (expanded) {
				icon = getRessourceIcon("Folder-Open-Green24x24.png");
			} else {
				icon = getRessourceIcon("Generic-Green-24x24.png");
			}
			
			CatalogTreeNode ctn = (CatalogTreeNode)value;
			String description = ctn.getCatalog().getDescription();
			if(description != null && description.equals(""))
				description = null;
			dtcr.setToolTipText(description);
		/* reviewer OR review-aspects */
		} else if(value instanceof AspectsTreeNode){
			AspectsTreeNode atn = (AspectsTreeNode)value;
			
			/* review aspects */
			if(atn.getParent() == null) {
				if (expanded) {
					icon = getRessourceIcon("Folder-Open-Green24x24.png");
				} else {
					icon = getRessourceIcon("Generic-Green-24x24.png");
				}
			/* reviewer */
			} else {
				if (expanded) {
					icon = getRessourceIcon("Administrator-24x24.png");
				} else {
					icon = getRessourceIcon("Preppy-24x24.png");
				}
			}
		/* category */
		} else if(value instanceof CategoryTreeNode) {
			
			if (expanded) {
				icon = getRessourceIcon("Folder-Open-24x24.png");
			} else {
				icon = getRessourceIcon("Generic-Yellow-24x24.png");
			}
			
		/* aspect */
		} else if(value instanceof AspectTreeNode) {
			icon = getRessourceIcon("Document-24x24.png");
			
			AspectTreeNode atn = (AspectTreeNode)value;
			String description = atn.getAspect().getDescription();
			if(description != null && description.equals(""))
				description = null;
			dtcr.setToolTipText(description);
		}
		
		/* apply icon */
		if(icon != null)
			dtcr.setIcon(icon);		
		
		/* return label */
		return dtcr;
	}
	
	private ImageIcon getRessourceIcon(String name) {
		return new ImageIcon(getClass().getResource("images/" + name));
	}

}
